<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends Controller{
	var $title='news';
	var $view='news';
	var $table='news';
	
	function News(){
		parent::Controller();	
		$this->load->model("dataset_atom","my_model");
		$this->my_model->table_name($this->table);
	}
	
	function index(){
		include_once 'inc/Init.php';
		$this->load->library('pager');
		$this->load->database();
		$where=array('type_id'=>$kind_id,'show'=>1);
		$page_get=intval($this->uri->segment(6,1));
		$page_post=$this->input->post('page');
		if(!empty($page_post)){
			$page=$page_post;
		}else{
			$page=$page_get;
		}
		$each=12;
		$this->pager->init(array(
			'table'=>$this->my_model->table_name(),
			'link'=>site_url("{$this->title}/index/$kind_url/{page}"),
			'where'=>$where,
			'page'=>$page,
			'each'=>$each,
		));
		$this->db->select('*');
		$this->db->or_where("(`top` IS NULL or `top`=0)");
		$this->db->order_by("sort_id","desc");
		list($page_link,$data,$page,$total,$max_page)=$this->pager->create_link();

		//头条新闻
		$topnews=$this->db->select('*')->order_by('sort_id','desc')->get_where($this->table,array('type_id'=>$kind_id,'show'=>1,'top'=>1))->row();
		
		$output='';

		$this->load->view($this->view,get_defined_vars());

	}
	
	function search(){
		include_once 'inc/Init.php';
		if(!$kw=trim($_POST['keywords'])){
			return goto_message('请输入关键字');			
		}
		else
		{
			$cond=myBase64Encode(myEscape($kw));
			$url='news.php?result/'.$kind_url.'/1/'.$cond;
		 	//header("Location:".$url."");
			echo '<script>window.location.href="'.$url.'"</script>';
		}
	}
	

	function result(){
		include_once 'inc/Init.php';
		$this->load->library('pager');
		$this->load->database();
		$where=array('show'=>1);
		$page=intval($this->uri->segment(6,1));
		$cond=$this->uri->segment(7,1);
		$each=6;
		$this->pager->init(array(
			'table'=>$this->my_model->table_name(),
			'link'=>site_url("{$this->title}/result/$kind_url/{page}/".$cond),
			'where'=>$where,
			'page'=>$page,
			'each'=>$each,
		));
		
		$this->db->select('*');
		if($cond=$this->uri->segment(7,1)){
			$kw=myUnescape(myBase64Decode($cond));
			$this->db->like('title',$kw);
			//$this->db->or_like('content',$kw);
		}
		$this->db->order_by("sort_id","desc");
		list($page_link,$data,$page,$total,$max_page)=$this->pager->create_link();
		$this->load->view($this->view,get_defined_vars());
	}
	
	function info(){
		include_once 'inc/Init.php';
		$id=$this->uri->segment(6,-1);
		if($id<1){
			return goto_message($this->lang->line('item_not_found'),'index');
		}
		$this->load->database();
		if(!($rs=$this->my_model->get($id))){
			return goto_message($this->lang->line('item_not_found'),'index');
		}
		
		//点击率
/*		$this->load->library('session');
		$click=$rs->click;
		if(!($this->session->userdata($id)))
		{
			$click++;
			$data=array('click'=>$click);
			$this->db->where('id',$rs->id);
			$this->db->update($this->table,$data);
			$this->session->set_userdata(array($id=>1));
		}*/
		
		$sub_title=strip_tags($rs->title); //格式化title
		$sort_id=$rs->sort_id;
		$type_id=$rs->type_id;
		list($prevItem,$nextItem)=$this->_prev_next_item($sort_id,$type_id);
		$this->load->view("{$this->view}_info",get_defined_vars());
	}
	
	function _prev_next_item($sort_id,$type_id){
		$this->load->database();
		$where=array('type_id'=>$type_id,'show'=>1);
		$this->db->where("sort_id>",$sort_id,false);
		$this->db->order_by("sort_id","asc");
		$prev=$this->db->get_where($this->table,$where,1);
		
		$this->db->where("sort_id<",$sort_id,false);
		$this->db->order_by("sort_id","desc");
		$next=$this->db->get_where($this->table,$where,1);
		
		return array($prev->row(),$next->row());
	}
}